package com.wgw.first.user.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wgw.first.R;
import com.wgw.first.user.adapter.OrderAdapter;
import com.wgw.first.user.bean.OrderBean2;
import com.wgw.first.utils.Constants;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * @author Admin
 * @version $Rev$
 * @des ${TODO}
 * @updateAuthor $Author$
 * @updateDes ${TODO}
 */
public class AllOrderActivity extends Activity {

    private OrderBean2 orderBean2;
    public  Boolean status = true;
    private ListView lv_order;
    private List<OrderBean2.Order> all_order;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        all_order = new ArrayList<OrderBean2.Order>();

        setContentView(R.layout.activity_all_order);
        lv_order = (ListView) findViewById(R.id.lv_order);
        System.out.println("all_order" + all_order);
        getDataFromNet();
    }

    public void getDataFromNet() {
        OkHttpUtils
                .get()
                .addParams("username", Constants.ALEADYLOGIN)
                .url(Constants.BASE+Constants.DOWNORDERDATA)
                //.url("http://192.168.191.1:8080/Product/HomeServlet")
                .id(100)
                .build()
                .execute(new MyStringCallback());
    }
    public class MyStringCallback extends StringCallback {

        @Override
        public void onError(Call call, Exception e, int id) {
            Log.e("TAG", "联网失败" + e.getMessage());
        }

        @Override
        public void onResponse(String response, int id) {

            switch (id) {
                case 100:
                    if (response != null) {
                        System.out.println("下载下来的订单数据：" + response);
                        //如果response为空，就不解析了
                        //对response进行json解析
                        int a = response.indexOf("{");
                        int b = response.lastIndexOf("}");
                        System.out.println("a:"+a);
                        System.out.println("b:"+b);
                        System.out.println("111" + response.substring(a+1,b).trim());
                        Boolean boo = !response.substring(a+1,b).trim().equals("");
                        System.out.println(boo);
                        if(!response.substring(a+1,b).trim().equals("")){
                            processData(response);
                            all_order =orderBean2.getOrder();
                            OrderAdapter orderAdapter = new OrderAdapter(all_order,AllOrderActivity.this);
                            lv_order.setAdapter(orderAdapter);


                        }else{
                            status = false;
                        }

                    }
                    break;
                case 101:
                    //                    Toast.makeText(mContext, "https", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }

    private void processData(String json) {

        if (!TextUtils.isEmpty(json)) {
            JSONObject OrderBean2 = JSON.parseObject(json, com.wgw.first.user.bean.OrderBean2.class);

            String order = OrderBean2.getString("order");

            orderBean2 = new OrderBean2();

            List<OrderBean2.Order> orders = JSON.parseArray(order, com.wgw.first.user.bean.OrderBean2.Order.class);
            orderBean2.setOrder(orders);

            System.out.println(orderBean2.toString());
        }

    }
}
